// Utility.h <huipeng_lhp@163.com 2007-12-12 14:47>
#pragma once
#include <assert.h>
#include <atlstr.h>

#if !defined(_UTILITY_H__2BD228C0_1623_11D9_AA32_FD45314BFD0D__INCLUDED_)
#define _UTILITY_H__2BD228C0_1623_11D9_AA32_FD45314BFD0D__INCLUDED_

class CCritSec
{
public:
	CCritSec() { InitializeCriticalSection(&m_CriticalSection); }
	~CCritSec() { DeleteCriticalSection(&m_CriticalSection); }
	void Lock() { EnterCriticalSection(&m_CriticalSection); }
	void Unlock() { LeaveCriticalSection(&m_CriticalSection); }

private:
	CRITICAL_SECTION m_CriticalSection;
};

class CLock
{
public:
	CLock(CCritSec* pCritSec) { assert(pCritSec); m_pCritSec=pCritSec; }
	~CLock() { m_pCritSec->Unlock(); }
	void Lock() { m_pCritSec->Lock(); }
	void Unlock() { m_pCritSec->Unlock(); }

private:
	CCritSec*	m_pCritSec;
};

inline int rndint( int min, int max )
{
	//srand(GetTickCount());
	return (min + rand() % (max - min + 1));
}

RECT*	CreateRect(RECT* pRect, LONG x, LONG y, LONG width, LONG height);
void	OriginRect(RECT* pRect);
LONG	RectWidth(const RECT* pRect);
LONG	RectHeight(const RECT* pRect);

CString GetLastErrorText(DWORD code);


BOOL SetClipboardData2(HWND hWnd, LPCTSTR data);


#endif // !defined(_UTILITY_H__2BD228C0_1623_11D9_AA32_FD45314BFD0D__INCLUDED_)
